package com.indigon.parkStory.control
{
	import com.indigon.parkStory.data.levels.LevelData;
	import com.indigon.parkStory.model.ModelMain;
	import com.indigon.parkStory.view.SoundMain;
	import com.indigon.parkStory.view.ViewMain;
	import com.indigon.utils.SoundManager;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setTimeout;

	public class ControlMain
	{
		private var _model		   : ModelMain;
		private var _view  		   : ViewMain;
		private var _sound		   : SoundMain;
		private var _stage 		   : Stage;
		private var _centerOffset  : Point;
		
		private var _currentLevel  : LevelData;
		
		private var _summaryScreen : SummaryScreen; 		
		private var _controlMenu   : ControlMenu;
		
		private var _endFlag  : Boolean;
		public  var pauseFlag : Boolean;
		
		public function ControlMain(model:ModelMain, view:ViewMain, sound:SoundMain, stage:Stage)
		{
			this._model = model;
			this._view  = view;
			this._sound = sound;
			this._stage = stage;
			
			this._centerOffset = new Point(stage.stageWidth * 0.5, stage.stageHeight * 0.5);
			
			this._view.init(_centerOffset, stage);
			
			_controlMenu = new ControlMenu(this, stage.stageWidth, stage.stageHeight);
			_controlMenu.set(stage, 0, 0, stage.numChildren);
			
			_summaryScreen = new SummaryScreen(this, stage, _centerOffset);
		}
		
		public function reset():void
		{
			_model.reset();
			_view.reset();
			
			_endFlag = false;
			
			_controlMenu.reset();
		}
		
		public function config(level:LevelData):void
		{
			_model.config(level);
			_view.config(level);
			
			_currentLevel = level;
		}
		
		public function start():void
		{
			_model.start();
			_view.start();
			_sound.start();
			
			_controlMenu.start();
			
			startLoop();
		}
		
		public function end():void
		{
			stopLoop();
			_model.end();
			_view.end();
			_sound.end();
			
			_controlMenu.end();
		}
		
		public function restart():void
		{
			reset();
			config(_currentLevel);
			start();
		}
		
		public function update(e:Event=null):void
		{
			_model.update();
			_view.update();
			_sound.update();
			_model.clearEvents();
			
			if (!_endFlag)
			{
				if (_model.defeatMet)
				{
					_endFlag = true;
					_controlMenu.graphic.visible = false;
					deactivate();
					setTimeout(_summaryScreen.showDefeat, SummaryScreen.FAILURE_DELAY);
				}
				else if (_model.victoryMet)
				{
					_endFlag = true;
					_controlMenu.graphic.visible = false;
					deactivate();
					submitScore();
					setTimeout(_summaryScreen.showVictory, SummaryScreen.SUCCESS_DELAY, _model.score);
				}
			}
		}
		
		public function activate():void
		{
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
		}
		
		public function deactivate():void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
		}
		
		private function stopLoop():void
		{
			_stage.removeEventListener(Event.ENTER_FRAME, update);
			deactivate();
			pauseFlag = true;
		}
		
		private function startLoop():void
		{
			_stage.addEventListener(Event.ENTER_FRAME, update);
			activate();
			_model.resume();
			
			pauseFlag = false;
		}
		
		public function pause():void
		{
			stopLoop();
			_controlMenu.set(_stage, 0, 0, _stage.numChildren - 1);
		}
		
		public function resume():void
		{
			startLoop();
		}
		
		public function restartSession():void
		{
			_stage.removeEventListener(MouseEvent.MOUSE_DOWN, restartSession);
			end();
			restart();
		}
		
		public function submitScore():void
		{
			var score : uint = _model.score.finalScore;
			var rank  : uint = _model.score.finalRank;
		}
		
		public function gotoIndexScreen():void
		{
			// TODO.
		}
		
		public function gotoNextLevel():void
		{
			// TODO.
		}
		
		private function mouseDown(e:MouseEvent=null):void
		{
			_model.frog.setJumpTo(e.stageX - _centerOffset.x, e.stageY - _centerOffset.y);
			e.stopImmediatePropagation();
		}
		
	}
}